//
//  NSAttributedString+Extension.swift
//  Weibo
//
//  Created by itteacher on 2017/8/3.
//  Copyright © 2017年 itteacher. All rights reserved.
//

import UIKit

extension NSAttributedString {
    //  根据表情模型创建表情富文本
    class func attributedStringWith(emoticon: HMEmoticon, font: UIFont) -> NSAttributedString {
        //  1. 创建图片对象
        let image = UIImage(named: emoticon.path!, in: HMEmoticonTools.sharedTools.emoticonBundle, compatibleWith: nil)
        //  2. 创建文本附件
        let textAttachment = HMTextAttachment()
        //  关联文本附件对应的表情模型
        textAttachment.emoticon = emoticon
        //  设置文本附件所需要的图片
        textAttachment.image = image
        //  获取文字的行高
        let lineHeight = font.lineHeight
        //  设置文本附件的bounds， 设置bunds会影响子控件的显示位置
        textAttachment.bounds = CGRect(x: 0, y: -4, width: lineHeight, height: lineHeight)
        //  3. 根据文本附件创建富文本
        let attrbutedStr = NSAttributedString(attachment: textAttachment)
        return attrbutedStr
    }
}
